<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use DB;
use Mail;

class RegisterController extends Controller
{
    
    //注册页面
    public function register()
    {
    	return view('home.email');
    }

    public function doRegister(Request $request)
    {
    	//自定义错误信息
    	$message = [
    	'userMail.required' => 'Email不能为空',
    	'userPass.required' => '密码不能为空',
    	'confirmed' => '两次输入密码不一致',
    	'between' => '密码由8-32位字符组成',
    	'unique' => '邮箱不可用',
    	'required' => '验证码不能为空',
    	];

    	// 设置验证规则
    	$this->validate($request,[
    			'userPass' => 'required|confirmed|between:8,32',
    			'userEmail' => 'required|unique:storeUser',
    			'myVerify' => 'required',
    		],$message);
    	
    	//判断验证码
    	if ($request->input('myVerify') == session('verify') ) {

    	//获取数据
    	$data = $request->except('_token','userPass_confirmation','myVerify');
    	
    	$data['userName'] = $data['userEmail'];
 
    	$data['userPass'] = md5($data['userPass']);
    	
    	//插入数据库
    	$row = DB::table('storeuser')->insert($data);
    	
    	 if ( $row ) {
            $ob = DB::table('storeuser')->where('userName',$request->input('userEmail'))->first();
    	 	session(['user' => $ob]);
          
  			return redirect('/');

    	 }
    		
    	} 
		
		return redirect('/uerRegister')->with('msg','验证码不正确');
    }

    public function ajax(request $request)
    {	
    		$email = $request->input('userEmail');
    	
    		//生成验证码
    		 $num = rand(100000,900000);
    		 $info = '欢迎注册华为商城'.$num;
    		 session(['verify' => $num]);
    		 session(['email' => $email]);
    		 
    		// //发送邮件
    		Mail::raw($info,function($message){

    			$message->subject('欢迎注册');
    			//发送给填写的邮箱
    			$message->to(session('email'));

    		});
    		
    }
}
